package cn.thornbird.orgsync.model.wechat;

import cn.thornbird.orgsync.model.AppUserData;
import cn.thornbird.orgsync.util.JsonUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class UserInfo extends UserList.User {

    public static final String CREATE_FIELDS = "userid,name,alias,mobile,department,order,position,gender,email," +
            "biz_mail,telephone,is_leader_in_dept,direct_leader,avatar_mediaid,enable,extattr,to_invite," +
            "external_profile,external_position,nickname,address,main_department";

    public static final String UPDATE_FIELDS = "userid,name,alias,mobile,department,order,position,gender,email," +
            "biz_mail,telephone,is_leader_in_dept,direct_leader,avatar_mediaid,enable,extattr,external_profile," +
            "external_position,nickname,address,main_department";

    /**
     * 成员头像的mediaid
     */
    @JsonProperty("avatar_mediaid")
    private String avatarMediaId;

    /**
     * 启用/禁用成员: 1-启用成员, 0-禁用成员
     */
    @JsonProperty("enable")
    private Integer enable;

    /**
     * 是否邀请该成员使用企业微信, 默认值为true
     */
    @JsonProperty("to_invite")
    private Boolean toInvite;

    /**
     * 视频号名字
     */
    @JsonProperty("nickname")
    private String nickname;

    public Map<String, Object> toCreateParams() {
        return toParams(CREATE_FIELDS);
    }

    public Map<String, Object> toUpdateParams() {
        return toParams(UPDATE_FIELDS);
    }

    public Map<String, Object> toParams(String fields) {
        return JsonUtil.toMap(this, StringUtils.isEmpty(fields) ? null : fields.split(","));
    }

    public UserInfo setId(String id) {
        setUserId(id);
        return this;
    }

    public UserInfo setDepartments(Map<String, AppUserData.DepartmentInfo> departments, String mainDepartmentId) {
        if (departments != null && !departments.isEmpty()) {
            setDepartment(new ArrayList<>());
            setOrder(new ArrayList<>());
            setIsLeaderInDepartment(new ArrayList<>());
            setDirectLeader(new ArrayList<>());
            for (Map.Entry<String, AppUserData.DepartmentInfo> entry: departments.entrySet()) {
                Long departmentId = null;
                try {
                    departmentId = Long.parseLong(entry.getKey());
                } catch (NumberFormatException e) {
                }
                if (departmentId != null) {
                    boolean isLeader = entry.getValue().getLeader() != null && entry.getValue().getLeader();
                    getDepartment().add(Long.parseLong(entry.getKey()));
                    getOrder().add(entry.getValue().getOrder());
                    getIsLeaderInDepartment().add(isLeader ? 1 : 0);
                }
            }
        }
        if (!StringUtils.isEmpty(mainDepartmentId)) {
            setMainDepartment(Long.parseLong(mainDepartmentId));
        }
        return this;
    }

    public UserInfo setLeaderId(String leaderId) {
        if (!StringUtils.isEmpty(leaderId)) {
            setDirectLeader(new ArrayList<>());
            getDirectLeader().add(leaderId);
        } else {
            setDirectLeader(null);
        }
        return this;
    }

}
